﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;


namespace DSQTransferService.Public
{
    public class HttpHelper
    {



        #region public static string OpenReadWithHttps(string URL, string strPostdata,int timeout, IPEndPoint ip ) 采用https协议访问网络
        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps(string url, string postdata, int timeout, IPEndPoint ip)
        {
            //return "网络异常";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";

                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }

                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }



        ///<summary>
        ///https
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttpsSafe(string url, string postdata, int timeout, IPEndPoint ip, WebHeaderCollection token = null)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
            //return "网络异常";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {


                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                if (token != null)
                {
                    request.Headers = token;
                }

                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }

                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }

        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps_ZF(string url, string postdata, int timeout, IPEndPoint ip)
        {
            //return "网络异常";
            System.Net.ServicePointManager.Expect100Continue = false;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }

                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }

        public static string OpenReadWithHttps_JD(string url, string postdata, int timeout, IPEndPoint ip, string RegisterNo, string Timestamp, string Signature)
        {
            //return "网络异常";
            //return "网络异常";
            System.Net.ServicePointManager.Expect100Continue = false;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);

                request.Headers.Add("JD-RegisterNo", RegisterNo);
                request.Headers.Add("JD-Timestamp", Timestamp);
                request.Headers.Add("JD-Signature", Signature);

                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }

                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }



        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps_TG(string url, string postdata, int timeout, IPEndPoint ip, string secret)
        {
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                //request.Timeout = 240000;
                request.Timeout = timeout;
                //request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
                //{
                //    return m_OutIPEndPoint;
                //};
                if (secret != null)
                {
                    request.Headers.Add("X-FLE-TOKEN", secret);

                }

                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/json";

                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {

                return "网络异常" + ex.Message;
            }
        }

        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps_GBK(string url, string postdata, int timeout, IPEndPoint ip)
        {
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.GetEncoding("GBK");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                //request.Timeout = 240000;
                request.Timeout = timeout;
                request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
                {
                    return m_OutIPEndPoint;
                };

                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/json";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GBK")))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {

                return "网络异常" + ex.Message;
            }
        }

        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="url">url地址</param>
        ///<param name="postdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps_UTF8(string url, string postdata, int timeout, IPEndPoint ip)
        {
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                //request.Timeout = 240000;
                request.Timeout = timeout;
                request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
                {
                    return m_OutIPEndPoint;
                };

                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/json";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {

                return "网络异常" + ex.Message;
            }
        }

        public static int HttpGet(string url, out string reslut)
        {
            reslut = "";
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Proxy = null;
                wbRequest.Method = "GET";

                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        reslut = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                reslut = e.Message;     //输出捕获到的异常，用OUT关键字输出
                return -1;              //出现异常，函数的返回值为-1
            }
            return 0;
        }
        public static string GetHttpResponse(string url, string postdata, int timeout, IPEndPoint ip, string account)
        {
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                //request.Timeout = 240000;
                request.Timeout = timeout;
                //request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
                //{
                //    return m_OutIPEndPoint;
                //};
                if (account != null)
                {
                    request.Headers.Add("account", account);

                }

                request.CookieContainer = new CookieContainer();
                request.Method = "GET";
                request.Accept = "*/*";
                request.ContentType = "application/json";

                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {

                return "网络异常" + ex.Message;
            }

        }

        public static string OpenReadWithHttps(string url, string postdata, int timeout, IPEndPoint ip, WebHeaderCollection token)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            //return "网络异常";
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                if (token != null)
                {
                    request.Headers = token;
                }
                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/json";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }

                return sb.ToString();
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }


        public static string OpenReadWithHttpsForm(string url, string postdata, int timeout, IPEndPoint ip)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            //return "网络异常";
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);

                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }

                return sb.ToString();
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }


        public static string OpenReadWithHttpsByGetMethod(string url, int timeout, IPEndPoint ip, WebHeaderCollection token)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            //return "网络异常";
            try
            {
                IPEndPoint m_OutIPEndPoint;
                m_OutIPEndPoint = ip;
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                if (token != null)
                {
                    request.Headers = token;
                }

                request.Proxy = null;
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "Get";
                request.Accept = "*/*";
                request.ContentType = "application/json";
                response = (HttpWebResponse)request.GetResponse();
                StringBuilder sb = new StringBuilder();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    const int length = 1024;
                    char[] c = new char[length];
                    int index = reader.Read(c, 0, c.Length);
                    while (index > 0)
                    {
                        var list = c.ToList();
                        list.RemoveAll(c_ => c_ <= 0);
                        c = list.ToArray();
                        sb.Append(c);
                        c = new char[length];
                        index = reader.Read(c, 0, c.Length);
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }


        public static string OpenReadWithHttps(string url, string postdata, int timeout, string apiID, string apiSecret)
        {

            string sigStr = "";
            string joinstr = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                string timestamp = getTimeTicks(DateTime.Now).ToString();
                string nonce = System.Guid.NewGuid().ToString("N");
                joinstr = genjoinstr(timestamp, nonce, apiID);
                sigStr = getAuthorization(apiID, apiSecret, timestamp, nonce, joinstr);
                request.Headers.Add("Authorization", sigStr);
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "POST";
                request.Accept = "*/*";
                request.ContentType = "application/json";
                byte[] buffer = encoding.GetBytes(postdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }
                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message + ex.StackTrace + " 签名:" + sigStr + " joinstr:" + joinstr;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }

        public static string OpenReadWithHttpsByGetMethod(string url, int timeout, string apiID, string apiSecret)
        {

            string sigStr = "";
            string joinstr = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                Encoding encoding = Encoding.UTF8;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = null;
                string timestamp = getTimeTicks(DateTime.Now).ToString();
                string nonce = System.Guid.NewGuid().ToString("N");
                joinstr = genjoinstr(timestamp, nonce, apiID);
                sigStr = getAuthorization(apiID, apiSecret, timestamp, nonce, joinstr);
                request.Headers.Add("Authorization", sigStr);
                request.Timeout = timeout;
                request.CookieContainer = new CookieContainer();
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();
                string sb = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    sb = reader.ReadToEnd();
                }
                return sb;
            }
            catch (Exception ex)
            {
                return "网络异常" + ex.Message + ex.StackTrace + " 签名:" + sigStr + " joinstr:" + joinstr;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }


        static string Createsig(string joinstr, string secret)
        {
            secret = secret ?? "";
            var encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(joinstr);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }
        public static String genjoinstr(String timestamp, String nonce, String apiID)
        {

            List<string> ArrStr = new List<string>();
            string joinStr = string.Empty;
            if (timestamp.Length > 0)
            {
                ArrStr.Add(timestamp);
            }
            if (nonce.Length > 0)
            {
                ArrStr.Add(nonce);

            }
            if (apiID.Length > 0)
            {
                ArrStr.Add(apiID);
            }
            for (int i = 0; i < ArrStr.Count() - 1; i++)
            {
                for (int j = 0; j < ArrStr.Count() - 1 - i; j++)
                {
                    //if (ArrStr[j].CompareTo(ArrStr[j + 1]) > 0)
                    if (string.CompareOrdinal(ArrStr[j], ArrStr[j + 1]) > 0)
                    {
                        string temp = ArrStr[j];
                        ArrStr[j] = ArrStr[j + 1];
                        ArrStr[j + 1] = temp;

                    }
                }
            }
            StringBuilder aftersort = new StringBuilder();
            for (int i = 0; i < ArrStr.Count(); i++)
            {
                aftersort.Append(ArrStr[i]);
            }
            joinStr = aftersort.ToString();
            return joinStr;
        }
        public static String genjoinstr(String timestamp, String nonce, String apiID, bool old)
        {
            byte[] bTimestamp = System.Text.Encoding.ASCII.GetBytes(timestamp);
            byte[] bNonce = System.Text.Encoding.ASCII.GetBytes(nonce);
            byte[] bApiID = System.Text.Encoding.ASCII.GetBytes(apiID);
            List<string> ArrStr = new List<string>();
            string joinStr = string.Empty;
            if (bTimestamp.Count() > 0)
            {
                ArrStr.Add(timestamp);
            }
            if (nonce.Count() > 0)
            {
                ArrStr.Add(nonce);

            }
            if (apiID.Count() > 0)
            {
                ArrStr.Add(apiID);
            }
            int MaxLen = 0;
            for (int i = 0; i < ArrStr.Count() - 1; i++)
            {
                for (int j = 0; j < ArrStr.Count() - 1 - i; j++)
                {
                    if (ArrStr[j][0] > ArrStr[j + 1][0])
                    {
                        string temp = ArrStr[j];
                        ArrStr[j] = ArrStr[j + 1];
                        ArrStr[j + 1] = temp;

                    }
                }
            }
            StringBuilder aftersort = new StringBuilder();
            for (int i = 0; i < ArrStr.Count(); i++)
            {
                aftersort.Append(ArrStr[i]);
            }
            joinStr = aftersort.ToString();
            return joinStr;
        }
        public static long getTimeTicks(DateTime dt)
        {
            try
            {
                TimeSpan ts1 = new TimeSpan(dt.AddHours(-8).Ticks);
                TimeSpan ts2 = new TimeSpan(new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
                TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                long lngResult = (long)ts3.TotalMilliseconds;
                return lngResult;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static String getAuthorization(String apiID, String apiSecret, string timestamp, string nonce, string joinstr)
        {

            string signature = Createsig(joinstr, apiSecret);
            string authorization = "key=" + apiID + ",timestamp=" + timestamp + ",nonce=" + nonce + ",signature="
            + signature;
            return authorization;
        }

        #endregion
    }
}
